package com.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.AjxBean;
import com.bean.DwzPage;
import com.entity.Cj;
import com.entity.Member;
import com.entity.St;
import com.entity.Tk;
import com.entity.Yy;
import com.util.DBO;
/**
 * 子servlet
 *
 */
public class SubServlet {
    public SubServlet() {
        super();
        if (this.dbo == null) {
            this.dbo = new DBO();
        }
    }

    private DBO dbo;
    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    /**
     * 预约的增删改查
     * 
     * @param req
     * @param res
     * @param method
     * @throws ServletException
     * @throws IOException
     * @throws ParseException
     */
    protected void yycurd(HttpServletRequest req, HttpServletResponse res, String method)
            throws ServletException, IOException, ParseException {
        HttpSession session = req.getSession();
        if (method.equals("del")) {
            String id = req.getParameter("id");
            String sql = "delete from yy where id='" + id + "'";
            sendAjaxDone(req, res, sql);
        } else if (method.equals("edit")) {
            String id = req.getParameter("id");
            String sql = "select * from yy where id=" + id;
            Yy yy = (Yy) dbo.findObj(sql, Yy.class);
            req.setAttribute("yy", yy);
            req.getRequestDispatcher("admin/yy/edityy.jsp").forward(req, res);
        } else if (method.equals("add")) {
            String sql = "select * from tk ";
            List tks = dbo.findPageList(sql, Tk.class);
            req.setAttribute("tks", tks);
            req.getRequestDispatcher("menber/yy/addyy.jsp").forward(req, res);
        } else if (method.equals("addsave")) {
            String menber = req.getParameter("menber");
            String yyrqtemp = req.getParameter("yyrq");
            String title = req.getParameter("title");
            String sql = "insert into yy (title,yyrq,menber,cjrq) values ('" + title + "','" + yyrqtemp + "','" + menber
                    + "',now())";
            sendAjaxDone(req, res, sql);
        } else if (method.equals("list")) {
            String keywords = req.getParameter("keywords");
            String sql;
            if (keywords == null || "".equals(keywords)) {
                sql = "select * from yy ";
            } else {
                sql = "select * from yy where menber='" + keywords.trim() + "' or title='" + keywords.trim() + "'";
            }
            String sf = (String) session.getAttribute("sf");
            if ("2".equals(sf)) {
                pages(req, res, sql, keywords, Yy.class, "admin/yy/yylist.jsp");
            } else {
                pages(req, res, sql, keywords, Yy.class, "menber/yy/yylist.jsp");
            }

        } else if (method.equals("edtsave")) {
            String id = req.getParameter("id");
            String yystauts = req.getParameter("yystauts");
            String sql = "update yy set yystauts='" + yystauts + "' where id =" + id;
            sendAjaxDone(req, res, sql);
        }
    }
    /**
     * 成绩的增删改操作
     * @param req
     * @param res
     * @param method
     * @throws ServletException
     * @throws IOException
     * @throws ParseException
     */
    protected void cjcurd(HttpServletRequest req, HttpServletResponse res, String method)
            throws ServletException, IOException, ParseException {
        HttpSession session = req.getSession();
        if (method.equals("del")) {
            String id = req.getParameter("id");
            String sql = "delete from cj where id='" + id + "'";
            sendAjaxDone(req, res, sql);
        }  else if (method.equals("add")) {
            String sql = "select * from tk ";
            List tks = dbo.findPageList(sql, Tk.class);
            req.setAttribute("tks", tks);
            req.getRequestDispatcher("admin/cj/addcj.jsp").forward(req, res);
        } else if (method.equals("list")) {
            String sf=(String) session.getAttribute("sf");
            if("2".equals(sf)){
                String keywords = req.getParameter("keywords");
                String sql;
                if (keywords == null || "".equals(keywords)) {
                    sql = "select * from cj ";
                } else {
                    sql = "select * from cj where code='" + keywords.trim() + "' or tkid='" + keywords.trim() + "'";
                }
                pages(req, res, sql, keywords, Cj.class, "admin/cj/cjlist.jsp");
            }else if("1".equals(sf)){
                String usercode=(String) session.getAttribute("user");
                String sql = "select * from cj where code='"+usercode+"'";
                pages(req, res, sql, "", Cj.class, "menber/cj/cjlist.jsp");
            }

        } else if (method.equals("addsave")) {
            String code = req.getParameter("code");
            String sj = req.getParameter("sj");
            String tkid = req.getParameter("title");
            String score = req.getParameter("score");
            String sql = "insert into cj (tkid,sj,code,score) values ('" + tkid + "','" + sj + "','" 
            + code + "',"+score+")";
            sendAjaxDone(req, res, sql);
        } 
    }

    /**
     * 题目的增删改查
     * 
     * @param req
     * @param res
     * @param method
     * @throws IOException
     * @throws ServletException
     */
    protected void tmcurd(HttpServletRequest req, HttpServletResponse res, String method)
            throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (method.equals("del")) {
            String id = req.getParameter("id");
            String sql = "delete from st where id=" + id ;
            sendAjaxDone(req, res, sql);
        }else if (method.equals("delct")) {
            String id = req.getParameter("ctid");
            String sql = "delete from cjjl where id=" + id ;
            sendAjaxDone(req, res, sql);
        } else if (method.equals("edit")) {
            String id = req.getParameter("id");
            String sql = "select * from st where id=" + id;
            St tm = (St) dbo.findObj(sql, St.class);
            req.setAttribute("tm", tm);
            if ("1".equals(tm.getTmtype()) || 1 == tm.getTmtype()) {
                req.getRequestDispatcher("admin/tm/edittm1.jsp").forward(req, res);
            } else {
                req.getRequestDispatcher("admin/tm/edittm2.jsp").forward(req, res);
            }
        } else if (method.equals("csjj")) {//交卷并获取成绩，保存成绩，清楚session，转发页面等。
            String ifzc = req.getParameter("ifzc");//是否是自测，如果自测，则为1，错了要形成错题集
            List<St> zxkstm1List=(List<St>) session.getAttribute("zxkstm1List");
            List<St> zxkstm2List=(List<St>) session.getAttribute("zxkstm2List");
            Cj cj=(Cj) session.getAttribute("cj");
            session.removeAttribute("cj");
            session.removeAttribute("zxkstm1List");
            session.removeAttribute("zxkstm2List");
            req.setAttribute("zxkstm2List", zxkstm2List);
            req.setAttribute("zxkstm1List", zxkstm1List);
            req.setAttribute("cj", cj);
            String sql = "update cj set score="+cj.getScore()+" where id=" + cj.getId();
            dbo.comUp(sql);
            if(ifzc !=null && "1".equals(ifzc)){//自测考试
                req.getRequestDispatcher("menber/tm/zckswb.jsp").forward(req, res);
            }else{
                req.getRequestDispatcher("menber/tm/zxkswb.jsp").forward(req, res);
            }
        } else if (method.equals("zxks")) {// 在线考试
            String ksid = req.getParameter("ksid");
            String sql = null;
            if ("1".equals(ksid)) {
                sql = "select * from tk where id=" + 1;
            } else if ("4".equals(ksid)) {
                sql = "select * from tk where id=" + 2;
            }else if("2".equals(ksid)){
                sql = "select * from tk where id=" + 1;
            }else if("3".equals(ksid)){
                sql = "select * from tk where id=" + 2;
            }
            Tk tk = (Tk) dbo.findObj(sql, Tk.class);
            String sqltm1 = "select * from st where tmtype=" + 1;// 单选题总集合
            List<St> zxkstm1List = findtmlists(sqltm1, tk.getTm1score());// 在线考试单选题出题
            String sqltm2 = "select * from st where tmtype=" + 2;// 多选题总集合
            List<St> zxkstm2List = findtmlists(sqltm2, tk.getTm2score());// 在线考试多选题出题集合
            session.setAttribute("zxkstm1List", zxkstm1List);
            session.setAttribute("zxkstm2List", zxkstm2List);
            Cj cj=new Cj();
            String usercod=(String) session.getAttribute("user");
            cj.setCode(usercod);
            cj.setScore(0);
            cj.setSj(date);
            cj.setTkid(tk.getTitle());
            cj.setTkrealid(tk.getId());
            String cjsql="insert into cj (code,score,sj,tkid) values ('"+usercod
                    +"',"+ 0+",'"+date+"','"+tk.getTitle()+"')";
            dbo.comUp(cjsql);
            String cjidsql="select id from cj where sj='"+date+"' and code ='"+usercod+"'";
            int cjid=dbo.getCount(cjidsql);
            cj.setId(cjid);
            session.setAttribute("cj", cj);
            req.setAttribute("cjid", cjid);
            req.setAttribute("tkname", tk.getTitle());
            req.setAttribute("t1score", tk.getTm1score());
            req.setAttribute("t2score", tk.getTm2score());
            if ("1".equals(ksid) || "4".equals(ksid)) {
                req.getRequestDispatcher("menber/tm/zxks.jsp").forward(req, res);
            } else {
                req.getRequestDispatcher("menber/tm/zcks.jsp").forward(req, res);
            }
        } else if (method.equals("checka")) {//检测一个题目
            String id = req.getParameter("id");
            String lsda = req.getParameter("lsda");
            String ifzc = req.getParameter("ifzc");//是否是自测，如果自测，则为1，错了要形成错题集
            Cj cj=(Cj) session.getAttribute("cj");
            String sql = "select * from st where id=" + id;
            St tm = (St) dbo.findObj(sql, St.class);
            if(tm.getTmtype().equals("1") || tm.getTmtype() == 1){
                List<St> zxkstm1List=(List<St>) session.getAttribute("zxkstm1List");
                for(St st : zxkstm1List){
                    if(st.getId().equals(id) || st.getId() == Integer.parseInt(id)){
                        st.setLsda(lsda);
                        break;
                    }
                }
            }else if(tm.getTmtype().equals("2") || tm.getTmtype() == 2){
                List<St> zxkstm2List=(List<St>) session.getAttribute("zxkstm2List");
                for(St st : zxkstm2List){
                    if(st.getId().equals(id) || st.getId() == Integer.parseInt(id)){
                        st.setLsda(lsda);
                        break;
                    }
                }
                
            }
            if(lsda != null && lsda.equals(tm.getZq())){
                cj.setScore(cj.getScore()+tm.getFs());
                session.setAttribute("cj", cj);
            }else if(ifzc !=null && "1".equals(ifzc)){//自测考试，形成错题集。
                int userid=Integer.parseInt((String)session.getAttribute("userid"));
                String sqlcjjl="insert cjjl (menberid,tkid,tmid,cjid) values("+ userid
                +","+cj.getTkrealid()+","+tm.getId()+","+cj.getId()+")";
                dbo.comUp(sqlcjjl);
            }
        AjxBean ab = new AjxBean("200", "");
        req.setAttribute("ab", ab);
        req.getRequestDispatcher("ajaxDone.jsp").forward(req, res);
        }else if (method.equals("edtsave")) {
            String id = req.getParameter("id");
            String tm = req.getParameter("tm");
            String d1 = req.getParameter("d1");
            String d2 = req.getParameter("d2");
            String d3 = req.getParameter("d3");
            String d4 = req.getParameter("d4");
            String zq = req.getParameter("zq");
            String fs = req.getParameter("fs");
            String sql = "update st set tm='" + tm + "' ,d1 ='" + d1 + "',d2 ='" + d2 + "' ,d3='" + d3 + "' ,d4='" + d4
                    + "' ,zq='" + zq + "' ,fs=" + fs + " where id =" + id;
            sendAjaxDone(req, res, sql);
        } else if (method.equals("toadd")) {
            String lx = req.getParameter("lx");
            String sql = "select * from tk ";
            List tks = dbo.findPageList(sql, Tk.class);
            req.setAttribute("tks", tks);
            if ("1".equals(lx)) {
                req.getRequestDispatcher("admin/tm/addtm1.jsp").forward(req, res);
            } else if ("2".equals(lx)) {
                req.getRequestDispatcher("admin/tm/addtm2.jsp").forward(req, res);
            }
        }if (method.equals("ctks")) {
        	String sql1 = null,sql2=null;
        	String tklx = req.getParameter("tkid");
        	Integer userid=Integer.parseInt((String) req.getSession().getAttribute("userid"));
        	if("1".equals(tklx)){
        		sql1="select a.id as ctid,b.* from cjjl a join st b on a.tmid=b.id where a.menberid="+ userid +" and a.tkid=1 and b.tmtype =1";
        		sql2="select a.id as ctid,b.* from cjjl a join st b on a.tmid=b.id where a.menberid="+ userid +" and a.tkid=1 and b.tmtype =2";
        	}else if("4".equals(tklx)){
        		sql1="select a.id as ctid,b.* from cjjl a join st b on a.tmid=b.id where a.menberid="+ userid +" and a.tkid=2 and b.tmtype =1";
        		sql2="select a.id as ctid,b.* from cjjl a join st b on a.tmid=b.id where a.menberid="+ userid +" and a.tkid=2 and b.tmtype =2";
        	}
        	List<Object> zxkstm3List =dbo.findPageList(sql1,St.class);
        	List<Object> zxkstm4List =dbo.findPageList(sql2,St.class);
            session.setAttribute("zxkstm3List", zxkstm3List);
            session.setAttribute("zxkstm4List", zxkstm4List);
            req.setAttribute("tklx", tklx);
            req.getRequestDispatcher("menber/tm/ctks.jsp").forward(req, res);
        } else if (method.equals("addsave")) {
            String tkid = req.getParameter("tkid");
            String tkname = req.getParameter("tkname");
            String tm = req.getParameter("tm");
            String d1 = req.getParameter("d1");
            String d2 = req.getParameter("d2");
            String d3 = req.getParameter("d3");
            String d4 = req.getParameter("d4");
            String zq = req.getParameter("zq");
            String fs = req.getParameter("fs");
            String tmtype = req.getParameter("tmtype");
            String sql = "insert into st (tkid,tkname,tm,d1,d2,d3,d4,zq,fs,tmtype) values (" + tkid + ",'" + tkname
                    + "','" + tm + "','" + d1 + "','" + d2 + "','" + d3 + "','" + d4 + "','" + zq + "'," + fs + ","
                    + tmtype + ")";
            sendAjaxDone(req, res, sql);
        }

    }

    /**
     * 查找题目集合
     * 
     * @param req
     * @param res
     * @throws IOException
     * @throws ServletException
     */
    protected void findtmlist(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String keywords = req.getParameter("keywords");
        String sql;
        if (keywords == null || "".equals(keywords)) {
            sql = "select * from st ";
        } else {
            sql = "select * from st where tm like '%" + keywords.trim() + "%'";
        }
        pages(req, res, sql, keywords, St.class, "admin/tm/tmlist.jsp");

    }
    protected void ctlist(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        Integer id=Integer.parseInt((String) req.getSession().getAttribute("userid"));
        String sql="select a.id as ctid,b.* from cjjl a join st b on a.tmid=b.id where a.menberid="+id;
        pages(req, res, sql, "", St.class, "menber/tm/ctlist.jsp");

    }

    /**
     * 科目试卷的curd
     * 
     * @param req
     * @param res
     * @param method
     * @throws ServletException
     * @throws IOException
     */
    protected void tkcurd(HttpServletRequest req, HttpServletResponse res, String method)
            throws ServletException, IOException {
        if (method.equals("del")) {
            String id = req.getParameter("id");
            if ("1".equals(id) || "2".equals(id)) {
                AjxBean ab = new AjxBean("300", "操作失败,科目1和科目4为固定科目，不可删除！");
                req.setAttribute("ab", ab);
                req.getRequestDispatcher("ajaxDone.jsp").forward(req, res);
            } else {
                String sql = "delete from tk where id='" + id + "'";
                sendAjaxDone(req, res, sql);
            }
        } else if (method.equals("edit")) {
            String id = req.getParameter("id");
            String sql = "select * from tk where id=" + id;
            Tk tk = (Tk) dbo.findObj(sql, Tk.class);
            req.setAttribute("tk", tk);
            req.getRequestDispatcher("admin/dwz/edit.jsp").forward(req, res);
        } else if (method.equals("edtsave")) {
            String id = req.getParameter("id");
            String title = req.getParameter("title");
            int tm1score = Integer.parseInt(req.getParameter("tm1score"));
            int tm2score = Integer.parseInt(req.getParameter("tm2score"));
            String sql = "update tk set title='" + title + "' ,tm1score =" + tm1score + " ,tm2score =" + tm2score
                    + " ,tkscore=" + (tm2score + tm1score) + " where id =" + id;
            sendAjaxDone(req, res, sql);
        } else if (method.equals("addsave")) {
            String bz = req.getParameter("bz");
            String title = req.getParameter("title");
            String sql=null;
            if("1".equals(bz)){
                int tm1score = Integer.parseInt(req.getParameter("tm1score"));
                int tm2score = Integer.parseInt(req.getParameter("tm2score"));
                sql = "insert into tk (title,tm1score,tm2score,tkscore) values ('" + title + "'," + tm1score + ","
                        + tm2score + "," + (tm2score + tm1score) + ")";
            }else{
                int tkscore = Integer.parseInt(req.getParameter("tkscore"));
                sql="insert into tk (title,tkscore) values ('" + title + "'," +
                        tkscore + ")";
            }
            sendAjaxDone(req, res, sql);
        }

    }

    protected void findtklist(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String keywords = req.getParameter("keywords");
        String sql;
        if (keywords == null || "".equals(keywords)) {
            sql = "select * from tk ";
        } else {
            sql = "select * from tk where title='" + keywords.trim() + "'";
        }
        tklbpage(req, res, sql, keywords, Tk.class, "admin/dwz/tklist.jsp");
    }

    protected void menberservletdo(HttpServletRequest req, HttpServletResponse res, String method)
            throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (method.equals("delm")) {
            String id = req.getParameter("id");
            String sql = "delete from member where id='" + id + "'";
            sendAjaxDone(req, res, sql);
        } else if (method.equals("ifusem")) {
            String id = req.getParameter("id");
            String sql = "";
            String str = dbo.getString("select ifuse from member where id='" + id + "'");
            if (str.equals("1")) {
                sql = "update member set ifuse='2' where id='" + id + "'";
            } else {
                sql = "update member set ifuse='1' where id='" + id + "'";
            }
            sendAjaxDone(req, res, sql);
        } else if (method.equals("adminexit")) {// 退出登录
            session.removeAttribute("user");
            session.invalidate();
            req.getRequestDispatcher("login.jsp").forward(req, res);
        } else if (method.equals("edituser")) {//进入修改学生信息的页面。
            String userid = (String) session.getAttribute("userid");
            String sql = "select * from member where id=" + userid;
            Member m = (Member) dbo.findObj(sql, Member.class);
            req.setAttribute("userobj", m);
            req.getRequestDispatcher("menber/useredit.jsp").forward(req, res);
        } else if (method.equals("editmenbsave")) {//修改学生用户信息保存。
            String userid = (String) session.getAttribute("userid");
            String realname = req.getParameter("realname");
            String email = req.getParameter("email");
            String address = req.getParameter("address");
            String sex = req.getParameter("sex");
            String sql = "update member set sex='" + sex + "',realname='" + realname + "',email='" + email
                    + "',address='" + address + "' where id=" + userid;
            sendAjaxDone(req, res, sql);
        } else if ("uppwd".equals(method)) {//修改密码
            String username = (String) session.getAttribute("user");
            String oldpwd = req.getParameter("oldpwd");
            String newpwd = req.getParameter("newpwd");
            String sf = (String) session.getAttribute("sf");
            if ("2".equals(sf)) {
                String str = dbo.getString(
                        "select id from admin where username='" + username + "' and  password='" + oldpwd + "'");
                if (str == null) {
                    req.setAttribute("message", "原始密码信息错误！");
                    req.getRequestDispatcher("admin/index.jsp").forward(req, res);
                } else {
                    int flag = dbo
                            .comUp("update admin set password='" + newpwd + "' where username='" + username + "'");
                    if (flag == 200) {
                        req.setAttribute("message", "修改密码成功！");
                        req.getRequestDispatcher("admin/index.jsp").forward(req, res);
                    } else {
                        req.setAttribute("message", "修改密码失败！");
                        req.getRequestDispatcher("admin/index.jsp").forward(req, res);
                    }
                }
            } else {
                String str = dbo.getString(
                        "select id from member where username='" + username + "' and  password='" + oldpwd + "'");
                if (str == null) {
                    req.setAttribute("message", "原始密码信息错误！");
                    req.getRequestDispatcher("menber/index.jsp").forward(req, res);
                } else {
                    int flag = dbo
                            .comUp("update member set password='" + newpwd + "' where username='" + username + "'");
                    if (flag == 200) {
                        req.setAttribute("message", "修改密码成功！");
                        req.getRequestDispatcher("menber/index.jsp").forward(req, res);
                    } else {
                        req.setAttribute("message", "修改密码失败！");
                        req.getRequestDispatcher("menber/index.jsp").forward(req, res);
                    }
                }
            }

        }
    }

    private void login(int czbz, HttpSession session, HttpServletRequest req, HttpServletResponse res, String age,
            String username, String password) throws ServletException, IOException {
        if (age.equals("2")) {
            String str = dbo.getString(
                    "select id from admin where username='" + username + "' and  password='" + password + "'");
            if (str == null) {
                String str2 = dbo.getString(
                        "select id from admin where username='" + username + "'");
                if(str2 == null){
                    req.setAttribute("message", username + "账号或身份不对");
                }else{
                    req.setAttribute("message", username + "密码不对");
                }
                req.getRequestDispatcher("login.jsp").forward(req, res);
            } else {
                if (czbz == 0) {//管理员用户，如果czbz为0表示人工在登录，否则表示过滤器自动在验证是否登录。
                    session.setAttribute("userid", str);
                    session.setAttribute("user", username);
                    session.setAttribute("sf", age);
                    session.setAttribute("psw", password);
                }
                req.getRequestDispatcher("admin/index.jsp").forward(req, res);
            }
        } else {
            String str = dbo.getString("select id from member where username='" + username + "' and  password='"
                    + password + "'  and ifuse='1'");
            if (str == null) {
                String str2 = dbo.getString(
                        "select id from member where username='" + username + "'");
                if(str2 == null){
                    req.setAttribute("message", username + "账号或身份不对");
                }else{
                    req.setAttribute("message", username + "密码不对");
                }
                req.getRequestDispatcher("login.jsp").forward(req, res);
            } else {
                String str3 = dbo.getString(
                        "select id from member where username='" + username + "' and  password='"
                                + password + "'  and ifuse='1'");
                if(str3 == null){
                    req.setAttribute("message", username + "对不起，该账号已停用，请联系管理员启用。");
                    req.getRequestDispatcher("login.jsp").forward(req, res);
                }else{
                    if (czbz == 0) {//学生用户，如果czbz为0表示人工在登录，否则表示过滤器自动在验证是否登录。
                        session.setAttribute("userid", str);
                        session.setAttribute("user", username);
                        session.setAttribute("sf", age);
                        session.setAttribute("psw", password);
                    }
                    req.getRequestDispatcher("menber/index.jsp").forward(req, res);
                }
            }
        }
    }

    protected void mlogin(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        HttpSession session = req.getSession();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String age = req.getParameter("age");
        if (age != null && !"".equals(age)) {
            login(0, session, req, res, age, username, password);
        } else {
            age = (String) session.getAttribute("sf");
            username = (String) session.getAttribute("user");
            password = (String) session.getAttribute("psw");
            login(1, session, req, res, age, username, password);
        }

    }

    protected void regStudent(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String realname = request.getParameter("realname");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        try {
            if (username.equals("admin")) {
                request.setAttribute("message", "此账号唯一，请用其他账号");
                request.getRequestDispatcher("reg.jsp").forward(request, response);
            } else {
                String str = dbo.getString("select id from member where username='" + username + "'");
                if (str == null) {
                    int flag = dbo
                            .comUp("insert into member(username,password,realname,sex,age,address,email,regtime,ifuse) "
                                    + "values('" + username + "','" + password + "','" + realname + "','" + sex + "','"
                                    + age + "','" + address + "','" + email + "','" + date + "','1')");
                    if (flag == 2) {
                        request.setAttribute("message", "注册成功");
                        request.getRequestDispatcher("login.jsp").forward(request, response);
                    } else {
                        request.setAttribute("message", "注册失败");
                        request.getRequestDispatcher("reg.jsp").forward(request, response);
                    }
                } else {
                    request.setAttribute("message", "该用户名已存在！");
                    request.getRequestDispatcher("reg.jsp").forward(request, response);
                }
            }
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    protected void lost(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("message", "新密码已经发到注册邮箱！");
        try {
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void listmenber(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String keywords = req.getParameter("keywords");
        if (keywords == null || "".equals(keywords)) {
            findall(req, res);
            return;
        }
        String sql = "select * from member where username='" + keywords.trim() + "' or realname= '" + keywords.trim()
                + "'";
        pages(req, res, sql, keywords, Member.class, "admin/dwz/findlist.jsp");
    }

    protected void findall(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String sql = "select * from member ";
        pages(req, res, sql, "", Member.class, "admin/dwz/userlist.jsp");
    }

    protected void sendAjaxDone(HttpServletRequest req, HttpServletResponse res, String sql)
            throws ServletException, IOException {
        int flag = dbo.comUp(sql);
        AjxBean ab;
        if (flag == 200) {
            ab = new AjxBean("200", "操作成功！");
        } else {
            ab = new AjxBean("300", "操作失败！");
        }
        req.setAttribute("ab", ab);
        req.getRequestDispatcher("ajaxDone.jsp").forward(req, res);
    }

    /**
     * 分页函数
     * 
     * @param req
     * @param res
     * @param sql
     * @param keywords
     * @param clazz
     * @param target
     * @throws ServletException
     * @throws IOException
     */
    protected void pages(HttpServletRequest req, HttpServletResponse res, String sql, String keywords, Class clazz,
            String target) throws ServletException, IOException {
        String a = req.getParameter("currentPage");
        String b = req.getParameter("numPerPage");
        int numPerPage = 10;
        int currentPage = 1;
        if (a != null && !a.isEmpty()) {
            currentPage = Integer.parseInt(a);
        }
        if (b != null && !b.isEmpty()) {
            numPerPage = Integer.parseInt(b);
        }
        String sqltemp = sql + " limit 0,30";
        List mlist = dbo.findPageList(sqltemp, clazz);
        DwzPage dp = new DwzPage(currentPage, numPerPage, mlist.size());
        if (currentPage != 1) {
            sqltemp = sql + " " + dp.getBeginPageIndex() + "," + dp.getNumPerPage();
            mlist = dbo.findPageList(sqltemp, clazz);
        }
        dp.setList(mlist);
        dp.setKeywords(keywords);
        req.setAttribute("dwzPage", dp);
        req.getRequestDispatcher(target).forward(req, res);
    }
    /**
     * 自动根据科目的类别，是否为理论考试分类。
     * @param req
     * @param res
     * @param sql
     * @param keywords
     * @param clazz
     * @param target
     * @throws ServletException
     * @throws IOException
     */
    protected void tklbpage(HttpServletRequest req, HttpServletResponse res, String sql, String keywords, Class clazz,
            String target) throws ServletException, IOException {
        String a = req.getParameter("currentPage");
        String b = req.getParameter("numPerPage");
        int numPerPage = 10;
        int currentPage = 1;
        if (a != null && !a.isEmpty()) {
            currentPage = Integer.parseInt(a);
        }
        if (b != null && !b.isEmpty()) {
            numPerPage = Integer.parseInt(b);
        }
        String sqltemp = sql + " limit 0,30";
        List mlist = dbo.findPageList(sqltemp, clazz);
        for(Object tk:mlist){
            Tk t=(Tk)tk;
            if(t.getTm1score() != null){
                if(t.getTm1score()+t.getTm2score()<1){
                    t.setType(2);
                }else{
                    t.setType(1);
                }
            }
        }
        DwzPage dp = new DwzPage(currentPage, numPerPage, mlist.size());
        if (currentPage != 1) {
            sqltemp = sql + " " + dp.getBeginPageIndex() + "," + dp.getNumPerPage();
            mlist = dbo.findPageList(sqltemp, clazz);
        }
        dp.setList(mlist);
        dp.setKeywords(keywords);
        req.setAttribute("dwzPage", dp);
        req.getRequestDispatcher(target).forward(req, res);
    }


    /**
     * 根据sql和总分数，查出考试的题目集合
     * 
     * @param sql
     * @param score
     * @return
     */
    protected List<St> findtmlists(String sql, int score) {
        List<St> zxkstm1List = new ArrayList<St>();// 在线考试出题
        String sqltemp = sql + " limit 0,100";
        List mlist = dbo.findPageList(sqltemp, St.class);
        Random ran = new Random();
        boolean flag;
        int zfs = 0;
        for (Object tm : mlist) {
            if (zfs < score) {
                St st = (St) tm;
                flag = ran.nextBoolean();
                if (flag) {
                    zxkstm1List.add(st);
                    zfs += (st.getFs() == null ? 0 : st.getFs());
                }
            } else {
                break;
            }
        }
        if (zfs < score) {
            String sqltem = sql + " limit 100,1000";
            mlist = dbo.findPageList(sqltem, St.class);
            for (Object tm : mlist) {
                if (zfs < score) {
                    St st = (St) tm;
                    flag = ran.nextBoolean();
                    if (flag) {
                        zxkstm1List.add(st);
                        zfs += st.getFs();
                    }
                } else {
                    break;
                }
            }
        }
        while (zfs < score) {
            String sqlte = sql + " limit 0,100";
            mlist = dbo.findPageList(sqlte, St.class);
            for (Object tm : mlist) {
                if (zfs < score) {
                    St st = (St) tm;
                    zxkstm1List.add(st);
                    zfs += st.getFs();
                } else {
                    break;
                }
            }
        }
        return zxkstm1List;
    }

    
}
